package com.example.demo.userInformation;



import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.example.demo.firemain.data.*;

import java.text.ParseException;
import java.util.Date;

/**
 * @author KK
 */
public class UserInformationHandler {


	/**
	 * 将解析的数据封装成 DeviceSensorMessage
	 *
	 * @param deviceSensorMessage 设备传感器信息
	 * @throws Exception 异常
	 */
	public void handle(DeviceSensorMessage deviceSensorMessage) throws Exception {
		//这里的data 是应用数据单位的16进制字符串
		String data = deviceSensorMessage.getData();

		//有一种可能,别的厂家用的心跳包应用数据单位为空 ，所以这个要处理为空的数据
		if (StrUtil.isNotEmpty(data)) {
			int messageNumber = Integer.parseInt(data.substring(2, 4), 16);
			//上传建筑消防设施部件运行状态 -国标
			String raw = data.substring(4);
			if (data.startsWith(AnalysisConst.Number.ZERO_TWO)) {

				runPartByUser(messageNumber, deviceSensorMessage, raw);

			}

			//4040040001010a0d101b0915315abd082f0000000000000051000280010101000000000006000000020020202020202020202020202020202020202020202020202020202020202020202020202020202020cad6b6afb0b4c5a52020202020202020202020200a0d101b0915762323
			//上传用户信息传输装置操作信息  18 > 24
			if (data.startsWith(AnalysisConst.Number.ONE_EIGHT)) {

				userOperation(data, deviceSensorMessage);

			}
			//上传用户信息传输装置运行状态  21
			if (data.startsWith(AnalysisConst.Number.ONE_FIVE)) {
				runUser(deviceSensorMessage, data);

			}
			//上传建筑消防设施部件模拟量值
			if (data.startsWith(AnalysisConst.Number.ZERO_THIRD)) {
				analog(messageNumber, deviceSensorMessage, raw);

			}
			//心跳包
			if (data.startsWith(AnalysisConst.Number.ONE_C)) {
				userHeart(deviceSensorMessage);
			}

			//消防主机运行状态  //0101010001200a1609080b15
			if (data.startsWith(AnalysisConst.Number.ZERO_ONE)) {
				runFireMain(data, deviceSensorMessage);
			}
			//上传建筑消防操作信息//0401010101020b1609080b15
			if (data.startsWith(AnalysisConst.Number.ZERO_FOUR)) {
				fireMainOperation(data, deviceSensorMessage);

			}
		}
	}

	/**
	 * 模拟量
	 * @param messageNumber
	 * @param deviceSensorMessage
	 * @param raw
	 */
	public void analog(Integer messageNumber,DeviceSensorMessage deviceSensorMessage,String raw){
		for (int i=1;i<messageNumber;i++){

			String message = raw.substring(32 * i - 32, 32 * i);

			Analog analog = new Analog(message,deviceSensorMessage.getId());

			AnalogQuantity quantity = analog.getAnalogQuantity();
			JSONObject object = new JSONObject();

			String uuids = UUID.randomUUID().toString();
			object.put("logId", uuids);
			//翻译设备编号，将用户信息传输装置源地址当作主机号，   设备编号= 源地址+回路+地址
			object.put("deviceCode", analog.getDevice_id()+quantity.getFireMainCode() + "-" + quantity.getLoopNo() + "-" + quantity.getAddressNo());
			//消防主机新功能，加一个部件说明
			object.put("address","模拟量:"+quantity.getAnalogValue() );
			//加字段适应新版消防主机功能
			object.put("addressNo",quantity.getAddressNo());
			object.put("fireMainCode",quantity.getFireMainCode());
			object.put("statusName","模拟量上传");
			object.put("loopNo",quantity.getLoopNo());
			object.put("partTypeExplain","");
			object.put("partType",quantity.getPartType());
			object.put("systemType",quantity.getSystemType());
			object.put("userCode",deviceSensorMessage.getId());

			System.out.println(object.toString());

		}
	}


	/**
	 * 消防主机操作信息
	 * @param data
	 * @param deviceSensorMessage
	 */
	public void fireMainOperation(String data,DeviceSensorMessage deviceSensorMessage){
		FireMainStatusOperation fireMainStatusOperation = new FireMainStatusOperation(data, deviceSensorMessage.getId());
		JSONObject object = new JSONObject();
		object.put("userCode",deviceSensorMessage.getId());
		object.put("systemType",fireMainStatusOperation.getSystemType());
		object.put("systemCode",fireMainStatusOperation.getSystemCode());
		object.put("statusName",fireMainStatusOperation.getStatusName());
		object.put("status", 2);
		object.put("uploadTime",new Date());
		System.out.println(object.toString());
	}


	/**
	 * 消防主机运行数据
	 * @param data
	 * @param deviceSensorMessage
	 */
	public void runFireMain(String data,DeviceSensorMessage deviceSensorMessage){
		FireMainStatus operatorStatus = new FireMainStatus(data,deviceSensorMessage.getId());

		JSONObject object = new JSONObject();
		object.put("userCode",deviceSensorMessage.getId());
		object.put("systemType",operatorStatus.getSystemType());
		object.put("systemCode",operatorStatus.getSystemCode());
		object.put("statusName",operatorStatus.getStatus());
		object.put("status",2);
		object.put("uploadTime",new Date());
		System.out.println(object.toString());
	}






	/**
	 * 思迪-用传心跳包
	 * @param deviceSensorMessage
	 * @throws ParseException
	 */
	public void userHeart(DeviceSensorMessage deviceSensorMessage) {
		//这个心跳包不处理
		JSONObject object = new JSONObject();
		object.put("userCode",deviceSensorMessage.getId());
		object.put("statusName","正常");
		object.put("status",2);
		object.put("uploadTime",new Date());

		System.out.println(object.toString());


	}

	/**
	 * 用传操作状态
	 * @param data
	 * @param deviceSensorMessage
	 */
	public void userOperation(String data,DeviceSensorMessage deviceSensorMessage){
		OperatorStatus operatorStatus = new OperatorStatus(data,deviceSensorMessage.getId());
		JSONObject object = new JSONObject();

		object.put("userCode",deviceSensorMessage.getId());
		object.put("statusName",operatorStatus.getOperation_flag());
		object.put("status",2);//0故障 1报警 2正常
		object.put("uploadTime",new Date());

		System.out.println(object.toString());

	}


	/**
	 * 上传用户信息传输装置运行状态
	 * @param deviceSensorMessage
	 * @param data 原始数据
	 *
	 */
	public void runUser(DeviceSensorMessage deviceSensorMessage,String data){

		UserStatus operatorStatus = new UserStatus(data,deviceSensorMessage.getId());
		JSONObject object = new JSONObject();
		object.put("userCode",deviceSensorMessage.getId());
		object.put("statusName",operatorStatus.getType());
		object.put("status",2);
		object.put("uploadTime",new Date());

		System.out.println(object.toString());
	}



	/**
	 * 上传部件运行状态
	 * @param messageNumber
	 * @param deviceSensorMessage
	 * @param raw
	 */
	public void runPartByUser(int messageNumber,DeviceSensorMessage deviceSensorMessage,String raw){
		for (int i = 1; i <= messageNumber; i++) {
			String message = raw.substring(92 * i - 92, 92 * i);

			DeviceStatusGuoBiao deviceStatus = new DeviceStatusGuoBiao(message,deviceSensorMessage.getId());
			UserMessage u = deviceStatus.getUserMessage();
			JSONObject object = new JSONObject();
			String uuids = UUID.randomUUID().toString();
			object.put("logId", uuids);
			//翻译设备编号，将用户信息传输装置源地址当作主机号，   设备编号= 源地址+回路+地址
			object.put("deviceCode", deviceStatus.getDevice_id()+u.getFireMainCode() + "-" + u.getLoopNo() + "-" + u.getAddressNo());
			//消防主机新功能，加一个部件说明
			object.put("address", u.getPartExplain().replace(" ", ""));

			object.put("systemType",u.getSystemType());
			object.put("fireMainCode",u.getFireMainCode());
			object.put("partType",u.getPartType());
			object.put("loopNo",u.getLoopNo());
			object.put("addressNo",u.getAddressNo());
			object.put("statusName",u.getPartStat());
			object.put("partTypeExplain",u.getPartTypeExplain());
			object.put("userCode",deviceSensorMessage.getId());
			//具体业务系统处理

			System.out.println(object.toString());
		}
	}



}
